<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/5
 * Time: 6:40 PM
 */
namespace app\index\controller;
use think\Exception;
class My extends CoinBaseContorller
{


    /**
     *
     * @api {post} api/my/ModifyPwd  修改密码
     * @apiDescription   修改密码
     * @apiGroup my
     * @apiParam {string} helpcode 助记码
     * @apiParam {string} newpwd 新密码
     * @apiParam {string} phone 手机号
     * @apiSuccess {string}  data  返回空字符表示成功
     * @apiVersion 1.0.1
     */
   public  function  ModifyPwd(){
       $userid=$this->getuserGuid();
       if($userid=="-1"){
           $phone=$this->param("phone");
           $rows=$this->db_select("select guid from token_sys_user where phone='".$phone."'");
           if(count($rows)==0){
               return $this->json_data("","当前手机号不存在");
           }
           $userid=$rows[0]["guid"];
       }
       $helpcode=$this->param("helpcode");
       $newpwd=$this->param("newpwd");
       return $this->json_data("",$this->_ModifyPwd($userid,$helpcode,$newpwd));
   }

   function _ModifyPwd($userid,$helpcode,$pwd){
     $row= $this->db_select("select count(*) counts from token_sys_user where guid='".$userid."' and zjm_words='".$helpcode."'");
     if($row[0]["counts"]==0){
         return "助记码输入错误";
     }
       return $this->db_exec(array("update token_sys_user set  pwd='".$pwd."' where guid='".$userid."' and zjm_words='".$helpcode."'"));
   }


    /**
     *
     * @api {post} api/my/ModifyTransactionPwd  修改交易密码
     * @apiDescription   修改交易密码
     * @apiGroup my
     * @apiParam {string} helpcode 助记码
     * @apiParam {string} newpwd 新密码
     * @apiSuccess {string}  data  返回空字符表示成功
     * @apiVersion 1.0.1
     */
    public  function  ModifyTransactionPwd(){
        $userid=$this->getuserGuid();
        $helpcode=$this->param("helpcode");
        $newpwd=$this->param("newpwd");
        return $this->json_data("",$this->_ModifyTransactionPwd($userid,$helpcode,$newpwd));
    }

    function _ModifyTransactionPwd($userid,$helpcode,$pwd){
        $row= $this->db_select("select count(*) counts from token_sys_user where guid='".$userid."' and zjm_words='".$helpcode."'");
        if($row[0]["counts"]==0){
            return "助记码输入错误";
        }
        return $this->db_exec(array("update token_sys_user set  transaction_pwd='".$pwd."' where guid='".$userid."' and zjm_words='".$helpcode."'"));
    }

    /**
     *
     * @api {post} api/my/MyTokenInfo  我的代币信息
     * @apiDescription   我的代币信息
     * @apiGroup my
     * @apiSuccess {string} phone 手机
     * @apiSuccess {string} coinname 币名
     * @apiSuccess {string}  tokencount  数量
     * @apiVersion 1.0.1
     */
    public  function  MyTokenInfo(){
        $userid=$this->getuserGuid();
        $row= $this->db_select("select phone from token_sys_user WHERE guid='".$userid."'");
        $phone=$row[0]["phone"];

        $row=$this->db_select("select name from token_sys_coin WHERE iscustom=1");
        $coinname=$row[0]["name"];

        $counts=$this->_MyTokenCount($userid);
        $counts=$counts/pow(10,8);
        $array=array("phone"=>$phone,"coinname"=>$coinname,"tokencount"=>$counts);
        return $this->json_data($array,"");
    }

    function _MyTokenCount($userid){
        $counts=$this->GetUserCustomCoinCount($userid);
        return $counts;
    }

    /**
     *
     * @api {post} api/my/GetZJM  获取助记码
     * @apiDescription   获取助记码
     * @apiGroup my
     * @apiSuccess {string} phone 手机
     * @apiVersion 1.0.1
     */
    public  function GetZJM(){
        $userid=$this->getuserGuid();
        if($userid=="-1"){
            $phone=$this->param("phone");
            $rows=$this->db_select("select guid from token_sys_user where phone='".$phone."'");
            if(count($rows)==0){
                return $this->json_data("","当前手机号不存在");
            }
            $userid=$rows[0]["guid"];
        }

        $array=$this->_GetZJM($userid);
        return $this->json_data($array,"");
    }
    function _GetZJM($userid){
      $row= $this->db_select("SELECT zjm_words from token_sys_user where guid='".$userid."'");
      $array= explode(',',$row[0]["zjm_words"]);
      shuffle($array);
      return $array;
    }




    /**
     *
     * @api {post} api/my/GetEarnningInfo 收益说明
     * @apiDescription   收益说明
     * @apiGroup my
     * @apiSuccess {string}  data  说明
     * @apiVersion 1.0.1
     */
    public function GetEarnningInfo(){
        $row=$this->db_select("select value from token_sys_config WHERE guid='1'");
        $data="1:收益包括系统收益和好友收益
        2:系统收益为固定收益,每隔".$row[0]["value"]."天发放一次
        3:好友收益为每个好友都会有一定的收益回报";
        return $this->json_data($data,"");
    }
}